545. Boundary of Binary Tree




/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */


/*
1. One simple approach is to divide this problem into three subproblems- left boundary, leaves and right boundary.
 2.    Left Boundary: We keep on traversing the tree towards the left and keep on adding the nodes in the resres array, provided the current node isn't a leaf node. If at any point, we can't find the left child of a node, but its right child exists, we put the right child in the resres and continue the process. 
3.     We make use of a recursive function addLeaves(res,root), in which we change the root node for every recursive call. If the current root node happens to be a leaf node, it is added to the resres array. Otherwise, we make the recursive call using the left child of the current node as the new root. After this, we make the recursive call using the right child of the current node as the new root. 
4.     We perform the same process as the left boundary. But, this time, we traverse towards the right. If the right child doesn't exist, we move towards the left child. Also, instead of putting the traversed nodes in the resres array, we push them over a stack during the traversal. After the complete traversal is done, we pop the element from over the stack and append them to the resres array. 

*/
class Solution {
private:
    bool isLeaf(TreeNode* root)
    {
        return !root->left && !root->right;
    };


    void addLeaves(TreeNode* root,vector<int>& ans)
    {
        if(!root) return;
        if(isLeaf(root)) 
        {
            ans.push_back(root->val);
            return;
        }
        addLeaves(root->left,ans);
        addLeaves(root->right,ans);
    };
    
public:
    vector<int> boundaryOfBinaryTree(TreeNode* root) {
        vector<int> ans;
        if(!root) return ans;
        ans.push_back(root->val);
        
        TreeNode* t = root->left;
        while(t)
        {
            if(!isLeaf(t))
            {
                ans.push_back(t->val);
            }
            if(t->left) t = t->left;
            else t = t->right;
            //if(t->left) t = t->left;    // 死循环  root 1, left 2, right 3. t->left == NULL, t->right== NULL;t的值不能更新。
            //if(t->right) t= t->right;
        }
        
        //addLeaves(root,ans); only one node ,1,output 1,1 
        addLeaves(root->left,ans);
        addLeaves(root->right,ans);
        
        stack<int> st;
        t = root->right;
        while(t)
        {
            if(!isLeaf(t))
            {
                st.push(t->val);
            }
            if(t->right) t = t->right;
            else t= t->left;
        }
        
        while(!st.empty())
        {
            ans.push_back(st.top());
            st.pop();
        }
        
        return ans;
    }
};

猜你喜欢

转载自blog.csdn.net/bjzhaoxiao/article/details/80276957