Week 12 Binary Tree Inorder Traversal
94.Binary Tree Inorder Traversal
問題概述
Given a binary tree, return the inorder traversal of its nodes’ values.
Example
Input: [1,null,2,3]
1
\
2
/
3
Output: [1,3,2]
分析
二叉樹遍歷:
前序遍歷:根結點、左子樹、右子樹
中序遍歷:左子樹、根結點、右子樹
後序遍歷:左子樹、右子樹、根結點
題目要求輸出中序遍歷
用非遞迴的方法,需要用棧來做,思路是從根節點開始,先將根節點壓入棧,然後再將其所有左子結點壓入棧,然後取出棧頂節點,儲存節點值,再將當前指標移到其右子節點上,若存在右子節點,則在下次迴圈時又可將其所有左子結點壓入棧中。這樣就保證了訪問順序為左-根-右
程式碼
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> vector; if(!root) return vector; stack<TreeNode *> stack; stack.push(root); while(!stack.empty()) { TreeNode *pNode = stack.top(); if(pNode->left) { stack.push(pNode->left); pNode->left = NULL; } else { vector.push_back(pNode->val); stack.pop(); if(pNode->right) stack.push(pNode->right); } } return vector; } };
相關推薦
Week 12 Binary Tree Inorder Traversal
94.Binary Tree Inorder Traversal 問題概述 Given a binary tree, return the inorder traversal of its nodes’ values. Example Input: [1,nul
【LeetCode】【Python】Binary Tree Inorder Traversal
nod 不知道 otto div ack return integer neu else Given a binary tree, return the inorder traversal of its nodes‘ values. For example: Gi
Binary Tree Inorder Traversal--leetcode
tor 遞歸 最大 pos tracking data leetcode post 出棧 原題鏈接:https://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 題目大意:中序遍歷二叉樹 解題思路:中序遍
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 94:Binary Tree Inorder Traversal
jsb pro inorder spa rgb col iss consola add Given a binary tree, return the inorder traversal of its nodes‘ values. For example:
LeetCode Binary Tree Inorder Traversal
each leetcode tracking scrip ret add trac art https LeetCode解題之Binary Tree Inorder Traversal 原題 不用遞歸來實現樹的中序遍歷。 註意點: 無
94. Binary Tree Inorder Traversal
cnblogs val new nor code left pub node list public class Solution { public List<Integer> inorderTraversal(TreeNode root) {
[leetcode] Binary Tree Inorder Traversal
root script inorder iter des put rec pan left Given a binary tree, return the inorder traversal of its nodes‘ values. Example: Input: [
leetcode:(94) Binary Tree Inorder Traversal(java)
package HashTable; /** * 題目: * Given a binary tree, return the inorder traversal of its nodes' values. * 解題思路: * 從根節點開始,先將根節點壓入棧,然後再將其所
LeetCode94. 二叉樹的中序遍歷(Binary tree Inorder Traversal)
題目描述 給定一個二叉樹,返回它的中序 遍歷。 示例: 輸入: [1,null,2,3] 1 \ 2 / 3 輸出: [1,3,2] 進階: 遞迴演算法很簡單,你可以通過迭代演算法完成嗎? 遞迴思路: 每次先判斷樹是否為空,
[Swift]LeetCode94. 二叉樹的中序遍歷 | Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 \ 2 / 3 Outpu
LintCode - Binary Tree Inorder Traversal
解法一 recursion class Solution { public: vector<int> res; vector<int> inorderTraversal(TreeNode * root) { res.clear();
leetcode-94-二叉樹的中序遍歷(binary tree inorder traversal)-java
題目及測試 package pid094; import java.util.List; /*中序遍歷二叉樹 給定一個二叉樹,返回它的中序 遍歷。 示例: 輸入: [1,null,2,3] 1 \ 2 / 3 輸出: [1,3,2] 進階:
94. Binary Tree Inorder Traversal(+樹的遍歷非遞迴)
題意: 非遞迴中序遍歷。 樹的先序遍歷 vector<int> preorderTraversal(TreeNode* root) { stack<TreeNode*> s; vector<int> res; TreeNode *p =
#Leetcode# 94. Binary Tree Inorder Traversal
https://leetcode.com/problems/binary-tree-inorder-traversal/ Given a binary tree, return the inorder traversal of its nodes' values. Ex
LeetCode 94.Binary Tree Inorder Traversal (二叉樹的中序遍歷)
題目描述: 給定一個二叉樹,返回它的中序 遍歷。 示例: 輸入: [1,null,2,3] 1 \ 2 / 3 輸出: [1,3,2] 進階: 遞迴演算法很簡單,你可以通過迭代演算法完成嗎? AC C++ Solution
leetcode 94. Binary Tree Inorder Traversal
求樹的中序遍歷,遞迴很簡單,迴圈我用棧來實現,再用hashmap或者改變變數的值來記錄一下。遞迴0ms,beats 100%,迴圈:改變值,1ms,beats 59.19%,用hashmap 2ms= = 只放迴圈的吧。 /** * Definition for a binary tree nod
【leetcode】94.(Medium)Binary Tree Inorder Traversal
解題思路: morris遍歷 提交程式碼:回溯 class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> ans=n
【LeetCode】94. Binary Tree Inorder Traversal(C++)
地址:https://leetcode.com/problems/binary-tree-inorder-traversal/ 題目: Given a binary tree, return the inorder traversal of its nodes’ values.
JavaScript刷LeetCode -- 94. Binary Tree Inorder Traversal【Medium】
一、題目 Given a binary tree, return the inorder traversal of its nodes’ values. 二、題目大意 二叉樹的中序遍歷。 三、解題思路 遞迴 四、程式碼實現 const inorderT