C++ solution


  • 0
    G
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
    
        void PrintInner(TreeNode *r, vector<int> &V)
        {
            if(r == NULL)
            {
                return;
            }
            
            if(r->left == NULL && r->right == NULL)
            {
                V.push_back(r->val);
                return;
            }
            
            PrintInner(r->left,V);
            PrintInner(r->right,V);
        }
    
        void PrintRightBoundry(TreeNode *root,vector<int> &V)
        {
            if(root == NULL)
            {
                return;
            }
            
            if(root->right)
            {
                PrintInner(root->left,V);
                PrintRightBoundry(root->right,V);
            }
            else
            {
                PrintRightBoundry(root->left,V);
            }
            
            V.push_back(root->val);
        }
       
        void PrintLeftBoundry(TreeNode *root,vector<int> &V)
        {
            if(root == NULL)
            {
                return;
            }
            
            V.push_back(root->val);
            
            if(root->left)
            {
                PrintLeftBoundry(root->left,V);
                PrintInner(root->right,V);
            }
            else
            {
                PrintLeftBoundry(root->right,V);
            }
        }
       
    
        vector<int> 
        boundaryOfBinaryTree(TreeNode* root) 
        {   
            vector<int> Res;
            
            if(!root)
            {
                return (Res);
            }
            
            Res.push_back(root->val);
            
            PrintLeftBoundry(root->left,Res);
            PrintRightBoundry(root->right,Res);
            return (Res);
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.