資料結構-----Binary Tree Inorder Traversal (二叉樹的中序遍歷)
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?
思路:
根據中序遍歷原則:左根右的原則,運用遞迴來遍歷,當然也可以寫非遞迴版本,需要用到棧,以後我再附上非遞迴的程式碼。
以下程式碼是我在牛客網oj環境下編譯測試通過的:
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { vector<int> s;//定義一個容器 public: vector<int> inorderTraversal(TreeNode *root) { if(root==NULL) { return s; } inorderTraversal(root->left); s.push_back(root->val); inorderTraversal(root->right); return s; } };
相關推薦
LeetCode 144 Binary Tree Preorder Traversal(二叉樹前序遍歷)
Given a binary tree, return thepreordertraversal of its nodes' values. For example: Given binary tr
資料結構-----Binary Tree Inorder Traversal (二叉樹的中序遍歷)
Binary Tree Inorder Traversal (二叉樹的中序遍歷) 題目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example: Giv
LeetCode 145 Binary Tree Postorder Traversal(二叉樹的興許遍歷)+(二叉樹、叠代)
int truct fin for data- right class span popu 翻譯 給定一個二叉樹。返回其興許遍歷的節點的值。 比如: 給定二叉樹為 {1。 #, 2, 3} 1 2 / 3 返回
LeetCode | Binary Tree Level Order Traversal(二叉樹層序遍歷)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Gi
LeetCode 94 Binary Tree Inorder Traversal(二叉樹的中序遍歷)+(二叉樹、迭代)
翻譯 給定一個二叉樹,返回其中序遍歷的節點的值。 例如: 給定二叉樹為 {1, #, 2, 3} 1 \ 2 / 3 返回 [1, 3, 2] 備註:用遞
[LintCode] Binary Tree Level Order Traversal(二叉樹的層次遍歷)
描述 給出一棵二叉樹,返回其節點值的層次遍歷(逐層從左往右訪問) 樣例 給一棵二叉樹 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分層遍歷結果:
LeetCode:102. Binary Tree Level Order Traversal(二叉樹的層次遍歷)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given b
Binary Tree Level Order Traversal(二叉樹層序遍歷-儲存並返回結果集)
題目描述 Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given
LeetCode 199 Binary Tree Right Side View(二叉樹層序遍歷)
Given a binary tree, imagine yourself standing on therightside of it, return the values of the node
Tree UVA - 548(二叉樹遞歸遍歷)
out pac col sstream end 遍歷 std 二叉 con 題目鏈接:https://vjudge.net/problem/UVA-548 題目大意:給一顆點帶權(權值各不相同,都是小於10000的正整數)的二叉樹的中序遍歷和後序遍歷,找一個葉子結點使得它
劍指Offer——樹:把二叉樹列印成多行(二叉樹層序遍歷)
對於二叉樹的最好的解決辦法就是遞迴。遍歷方法無外乎先序遍歷,中序遍歷,後序遍歷方法以及層序遍歷方法。這裡給大家安利一個關於樹的面試題的連結,博主walkinginthewind比較全面且詳細的介紹了二叉樹相關的面試題:對於層序遍歷,最好的方法就是用佇列記錄遍歷節點的值,按層列
binary-tree-inorder-traversal——二叉樹中序遍歷
str () init inorder code while urn value public Given a binary tree, return the inordertraversal of its nodes‘ values. For example:Given
【LeetCode-面試算法經典-Java實現】【107-Binary Tree Level Order Traversal II(二叉樹層序遍歷II)】
lin -m length ret itl pub util 實現類 markdown 【107-Binary Tree Level Order Traversal II(二叉樹層序遍歷II)】 【LeetCode-面試算法經典-Java實現】【全
【LeetCode】Binary Tree Inorder Traversal 二叉樹中序遍歷遞迴以及非遞迴演算法
Total Accepted: 16494 Total Submissions: 47494 Given a binary tree, return the inorder traversal of its nodes' values. For example: Giv
LeetCode題解-144. Binary Tree Preorder Traversal(二叉樹的非遞迴前序遍歷)
題目:Given a binary tree, return the preorder traversal of its nodes' values.Example:Input: [1,null,2,3] 1 \ 2 / 3 Outpu
LeetCode | Binary Tree Level Order Traversal II(二叉樹層序遍歷II)
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
【LeetCode-面試演算法經典-Java實現】【145-Binary Tree Postorder Traversal(二叉樹非遞迴後序遍歷)】
原題 Given a binary tree, return the postorder traversal of its nodes’ values. For exampl
LeetCode 144 Binary Tree Preorder Traversal(二叉樹的前序遍歷)+(二叉樹、迭代)
翻譯 給定一個二叉樹,返回其前序遍歷的節點的值。 例如: 給定二叉樹為 {1,#, 2, 3} 1 \ 2 / 3 返回 [1, 2, 3] 備註:用遞迴
Leetcode 94. Binary Tree Inorder Traversalt (二叉樹中序遍歷)
原題 Given a binary tree, return the inorder traversal of its nodes’ values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Output
[LeetCode] Binary Tree Level Order Traversal II 二叉樹層序遍歷之二
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 ex