LeetCode二叉树遍历的问题

来源:互联网 发布:java 监听模式原理 编辑:程序博客网 时间:2024/04/30 05:10

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);    }
0 0
原创粉丝点击