1. 程式人生 > >LeetCode二叉樹遍歷的問題

LeetCode二叉樹遍歷的問題

1.二叉樹後序遍歷postordertraversal
非遞迴解法

Given a binary tree, return the postorder traversal of its nodes’ values.
For example:
Given binary tree{1,#,2,3},
1
\
2
/
3

return[3,2,1].

    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> res;//用來存結果
if(!root)return res; stack<TreeNode*> stack; stack.push(root); TreeNode* last; while(!stack.empty()) { TreeNode* curr=stack.top(); if((!curr->left&&!curr->right)||curr->left==last||curr->right==last)//如果當前節點是葉子結點或者是前一個輸出節點的父節點,就把它出棧並設定為前一個輸出的節點
{ stack.pop(); res.push_back(curr->val); last=curr; } else { if(curr->right)stack.push(curr->right); if(curr->left)stack.push(curr->left); } } return
res; }

2.二叉樹中序遍歷inordertraversal
Given a binary tree, return the inorder traversal of its nodes’ values.
For example:
Given binary tree{1,#,2,3},
1
\
2
/
3

return[1,3,2].

vector<int> InOrderTraversal(TreeNode* root)
{
    vector<int> path;
    if(!root)return path;
    TreeNode *p=root;
    stack<TreeNode*> stack;
    while(!stack.empty()||p)
    {
        if(p)
        {
            stack.push(p);
            p=p->left;
        }
        else
        {
            p=stack.top();
            stack.pop();
            path.push_back(p->val);
            p=p->right;
        }
    }
    return path;
}

3.二叉樹先序遍歷preordertraversal
非遞迴解法
Given a binary tree, return the preorder traversal of its nodes’ values.
For example:
Given binary tree{1,#,2,3},
1
\
2
/
3

return[1,2,3].

    vector<int> preorderTraversal(TreeNode *root) {
        vector<int> path;
        if(root==NULL)return path;
        stack<TreeNode*> stack;
        stack.push(root);
        while(!stack.empty())
            {
            TreeNode* cur=stack.top();
            stack.pop();
            path.push_back(cur->val);
            if(cur->right)
                stack.push(cur->right);
            if(cur->left)
                stack.push(cur->left);
        }
        return path;
    }

4.二叉樹層次遍歷LevelOrderTraversal
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).
For example:
Given binary tree{3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7

return its level order traversal as:
[
[3],
[9,20],
[15,7]
]

    vector<vector<int> > levelOrder(TreeNode *root) {
        vector<vector<int>> result;
        if(!root)return result;
        deque<TreeNode*> deque;//層次遍歷適合用佇列存
        deque.push_back(root);
        int count=1;//初始時deque裡有一個根節點
        while(!deque.empty())
            {
            vector<int> temp;//用於存每一層的結果的臨時vector
            TreeNode *curr;
            int tempcount=0;//用於記錄一層中有多少個節點
            while(count--)
                {
                curr=deque.front();
                deque.pop_front();
                temp.push_back(curr->val);
                if(curr->left)
                {
                    deque.push_back(curr->left);
                    tempcount++;
                }
                if(curr->right)
                {
                    deque.push_back(curr->right);
                    tempcount++;
                }
            }
            count=tempcount;//下一次把剛剛放入deque中的節點放到temp中,並加入他們的子節點
            result.push_back(temp);
        }
        return result;
    }

5.是否存在路徑和為某一值的路徑
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree andsum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path5->4->11->2which sum is 22.

    bool hasPathSum(TreeNode *root, int sum) {
        if(!root)return false;
        if(!root->left&&!root->right&&sum-root->val==0)
            return true;
        return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val);
    }