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;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值