LeetCode之“树”:Binary Tree Preorder Traversal && Binary Tree Inorder Traversal && Binary Tree Postorder Traversal

Binary Tree Preorder Traversal

  题目链接

  题目要求:

  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].

  Note: Recursive solution is trivial, could you do it iteratively?

递归解法

  对这道题,用递归是最简单的了,具体程序如下:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> preorderTraversal(TreeNode* root) {
13         vector<int> rVec;
14         preorderTraversal(root, rVec);
15         return rVec;
16     }
17
18     void preorderTraversal(TreeNode *tree, vector<int>& rVec)
19     {
20         if(!tree)
21             return;
22
23         rVec.push_back(tree->val);
24         preorderTraversal(tree->left, rVec);
25         preorderTraversal(tree->right, rVec);
26     }
27 };

非递归解法

  非递归解法相对就要难很多了,理解起来比较抽象。在这里我们需要借助。当左子树遍历完后,需要回溯并遍历右子树。具体程序如下:

 1 vector<int> preorderTraversal(TreeNode* root) {
 2     vector<int> rVec;
 3     stack<TreeNode *> st;
 4     TreeNode *tree = root;
 5     while(tree || !st.empty())
 6     {
 7         if(tree)
 8         {
 9             st.push(tree);
10             rVec.push_back(tree->val);
11             tree = tree->left;
12         }
13         else
14         {
15             tree = st.top();
16             st.pop();
17             tree = tree->right;
18         }
19     }
20
21     return rVec;
22 }

Binary Tree Inorder Traversal

  题目链接

  题目要求:

  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].

  Note: Recursive solution is trivial, could you do it iteratively?

  这道题的非递归解法跟上题的非递归解法基本一致,只是访问的节点时机不一样,具体程序如下:

 1 vector<int> inorderTraversal(TreeNode* root) {
 2     vector<int> rVec;
 3     stack<TreeNode *> st;
 4     TreeNode *tree = root;
 5     while(tree || !st.empty())
 6     {
 7         if(tree)
 8         {
 9             st.push(tree);
10             tree = tree->left;
11         }
12         else
13         {
14             tree = st.top();
15             rVec.push_back(tree->val);
16             st.pop();
17             tree = tree->right;
18         }
19     }
20
21     return rVec;
22 }

Binary Tree Postorder Traversal

  题目链接

  题目要求:

  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].

  Note: Recursive solution is trivial, could you do it iteratively?

时间: 2024-10-07 15:52:46

LeetCode之“树”:Binary Tree Preorder Traversal && Binary Tree Inorder Traversal && Binary Tree Postorder Traversal的相关文章

Leetcode 树 Binary Tree Preorder Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Preorder Traversal Total Accepted: 17948 Total Submissions: 51578 Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2

[LeetCode][JavaScript]Binary Tree Preorder Traversal

Binary Tree Preorder Traversal 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]. Note: Recursive solution is trivial, could you do it iteratively? https://leetcod

leetcode 刷题之路 79 Binary Tree Preorder Traversal

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]. Note: Recursive solution is trivial, could you do it iteratively? 非递归实现树的前序遍历.前序遍历比较简单,注意入栈的时候应该按照右孩子节点在前的顺序,这样

[LeetCode 题解]: Binary Tree Preorder Traversal

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]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

LeetCode: Binary Tree Preorder Traversal [144]

[题目] 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]. Note: Recursive solution is trivial, could you do it iteratively? [题意] 非递归返回先序遍历结果 [思路] 维护一个栈即可 [代码] /** *

[leetcode]Binary Tree Preorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先序遍历.先序遍历的遍历顺序是:根,左子树,右子树. 解题思路:如果树为下图: 1 /     \ 2         3 /     \    /    \ 4       5  6     7 使用一个栈.步骤为: 一,先遍历节点1,并入栈,如果有左孩子,继续遍历并入栈,一直到栈为{1,2,4}.

LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 题目标签:Array, Tree 这到题目和105 几乎是一摸一样的,唯一的区别就是把pre-order 换成 post-order.因为post-order是最后一个数字是root,所以要从右向左的遍历.还需要把helpe

[Leetcode][Tree][Binary Tree Preorder Traversal]

二叉树的前序遍历:root点先被访问,然后是left和right子节点.迭代的版本也相对好写. 1.递归版本:时间复杂度O(N),空间复杂度O(N) 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }