Binary Tree Inorder/Preorder 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].

OJ‘s Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where ‘#‘ signifies a path terminator where no node exists below.

Here‘s an example:

   1
  /  2   3
    /
   4
         5

The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

 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  * };
 9  */
10 class Solution {
11 public:
12     vector<int> inorderTraversal(TreeNode *root) {
13         vector<int> ret;
14         if(root == NULL)
15             return ret;
16
17         stack<TreeNode *> stack;
18         stack.push(root);
19
20         while(!stack.empty()){
21             TreeNode *node = stack.top();
22             stack.pop();
23             if(node->left == NULL && node->right == NULL){
24                 ret.push_back(node->val);
25             }
26             else{
27                 if(node->right != NULL)
28                     stack.push(node->right);
29                 stack.push(node);
30                 if(node->left != NULL)
31                     stack.push(node->left);
32
33                 node->left = node->right = NULL;
34             }
35
36         }
37
38         return ret;
39
40     }
41 };

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

和上面要求一样,只是要返回以中序方式序列的元素,这次用递归实现:

 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  * };
 9  */
10 class Solution {
11 public:
12     vector<int> preorderTraversal(TreeNode *root) {
13         vector<int> ret;
14         if(root == NULL)
15             return ret;
16         PreorderTraversal(root,ret);
17         return ret;
18     }
19
20     void PreorderTraversal(TreeNode *root,vector<int> &ret){
21         if(root != NULL){
22             ret.push_back(root->val);
23             PreorderTraversal(root->left,ret);
24             PreorderTraversal(root->right,ret);
25         }
26     }
27 };
时间: 2024-10-15 09:54:42

Binary Tree Inorder/Preorder Traversal 返回中序和前序/遍历二叉树的元素集合的相关文章

根据中序和前序遍历还原二叉树

思路就是从前序遍历出发,到中序遍历中找到相应的根节点,然后确定左子树和右子树的范围 struct BiNode { int value; BiNode *leftchild; BiNode *rightchild; }; class solution { public: BiNode* BiTree(int *preorder,int *midorder,int length) { if(length < 0 || preorder == nullptr || midorder == nullp

[Swift]LeetCode105. 从前序与中序遍历序列构造二叉树 | Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 / 9 20 /

LeetCode: Binary Tree Level Order Traversal II [107]

[题目] Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order

leetcode题解:Construct Binary Tree from Preorder and Inorder Traversal (根据前序和中序遍历构造二叉树)

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(

【LeetCode每天一题】Construct Binary Tree from Preorder and Inorder Traversal(使用前序和中序遍历构建二叉树)

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Return the following binary tree: 3 / 9 20 /

[LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 这道题要求用先序和中序遍历来建立二叉树,跟之前那道Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树原理基本相同,针对这道题,由于先

leetcode 题解: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)下面有两种实现:递归(Recursive )与非递归(迭代iterative

[LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal

题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路 主要是根据前序遍历和中序遍历的特点解决这个题目. 1.确定树的根节点.树根是当前树中所有元素在前序遍历中最先出现的元素. 2.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

leetcode------Construct Binary Tree from Preorder and Inorder Traversal

标题: Construct Binary Tree from Preorder and Inorder Traversal 通过率: 26.5 难度: 中等 Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 根据前序遍历和中序遍历构建二叉树:看一棵树如下: 1 / 2 4 \