【LeetCode】144. Binary Tree Preorder Traversal 二叉樹先序遍歷的非遞迴實現
題目:
翻譯:給定一個二叉樹,返回先序遍歷的序列。
分析:二叉樹的先序遍歷、中序遍歷及後序遍歷演算法是資料結構中最基礎的遍歷演算法。
先序遍歷:先訪問根節點的資料,再訪問左孩子節點的資料,最後訪問右孩子節點的資料。圖中例子先序遍歷輸出的序列為:【1,2,3】。
中序遍歷:先訪問左孩子節點的資料,再訪問根節點的資料,最後訪問右孩子節點的資料。圖中例子中序遍歷輸出的序列為:【1,3,2】。
後序遍歷:先訪問左孩子節點的資料,再訪問右孩子節點的資料,最後訪問根節點的資料。圖中例子後序遍歷輸出的序列為:【3,2,1】。
這三種遍歷演算法若用遞迴來寫是非常簡單的,但是如果採用非遞迴的方式來實現,相對比較複雜。
以先序遍歷為例,可以使用棧的資料結構來實現先序遍歷的非遞迴方法。具體操作如下:
1.若二叉樹為空,則返回空序列,結束;若不為空,轉2;
2.新建一個空棧,將根節點加入棧中;
3.若棧不為空,取出棧頂節點,訪問該節點的資料(將棧頂元素加入返回的序列中);
判斷該節點的【右節點】是否為空,若不為空,將該節點的右節點加入到棧中;
判斷該節點的【左節點】是否為空,若不為空,將該節點的左節點加入到棧中;
(注意:此處是先判斷右節點,再判斷左節點,因為棧的特點是“後進先出”,左節點總在右節點先遍歷,所以要後加入棧中。)
4.重複3,直到棧為空,返回最終得到的先序遍歷序列。
程式碼:
在下面的程式碼中使用了C#中的List來實現一個棧。
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ public class Solution { public IList<int> PreorderTraversal(TreeNode root) { List<int> results=new List<int>(); if(root==null) return results; List<TreeNode> stack=new List<TreeNode>();//新建一個佇列 stack.Add(root); while(stack.Count()!=0)//若棧不為空,取出棧頂元素,訪問數值,判斷左右孩子是否為空,若不為空,加入棧,先加右孩子,再加左孩子 { TreeNode temp=stack.Last();//取出棧頂元素 stack.Remove(stack.Last());//移除 results.Add(temp.val); if(temp.right!=null) stack.Add(temp.right); if(temp.left!=null) stack.Add(temp.left); } return results; } }
相關推薦
【LeetCode】144. Binary Tree Preorder Traversal 二叉樹先序遍歷的非遞迴實現
題目: 翻譯:給定一個二叉樹,返回先序遍歷的序列。 分析:二叉樹的先序遍歷、中序遍歷及後序遍歷演算法是資料結構中最基礎的遍歷演算法。 先序遍歷:先訪問根節點的資料,再訪問左孩子節點的資料,最後訪問右孩子節點的資料。圖中例子先序遍歷輸出的序列為:【
【LeetCode】Binary Tree Preorder Traversal 二叉樹前序遍歷遞迴以及非遞迴演算法
Total Accepted: 17403 Total Submissions: 50093 My Submissions Given a binary tree, return the preorder traversal of its nodes' values.
【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】Binary Tree Postorder Traversal 二叉樹後序遍歷遞迴以及非遞迴演算法
Total Accepted: 15614 Total Submissions: 50928 My Submissions Given a binary tree, return the postorder traversal of its nodes' values.
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 144. Binary Tree Preorder Traversal 二叉樹的前序遍歷 C++
do it emp 二叉 c++ pre value code val right Given a binary tree, return the preorder traversal of its nodes‘ values. Example: Input: [1
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 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
[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:Given binary tree {3,9
leetcode--589. N叉樹的前序遍歷 非遞迴實現
由於本人菜指標不熟,導致指標指的我頭疼,遞迴一邊先壓入另一邊,使得被遞迴的點最後壓入即可。最基本的非遞迴樹的DFS 程式碼 vector<int> preorder(Node* root) { vector<int> a; if(root!=NU
【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】145. Binary Tree Postorder Traversal
tor ron medium div lee har pos tco urn 題目如下: 解題思路:湊數題+3,搞不懂為什麽本題的難度是Hard,而【leetcode】590. N-ary Tree Postorder Traversal是Medium。 代碼如下: #
[Leetcode 144]二叉樹前序遍歷Binary Tree Preorder Traversal
public list left 前序 output nod roo strong while 【題目】 Given a binary tree, return the preordertraversal of its nodes‘ values. Example: Inp
leetcode question 144:Binary Tree Preorder Traversal
問題: Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,nu
【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.
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 144 Binary Tree Preorder Traversal(二叉樹前序遍歷)
Given a binary tree, return thepreordertraversal of its nodes' values. For example: Given binary tr
【LeetCode筆記】Balanced Binary Tree 高度平衡二叉樹
題目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tr
LeetCode 94.Binary Tree Inorder Traversal (二叉樹的中序遍歷)
題目描述: 給定一個二叉樹,返回它的中序 遍歷。 示例: 輸入: [1,null,2,3] 1 \ 2 / 3 輸出: [1,3,2] 進階: 遞迴演算法很簡單,你可以通過迭代演算法完成嗎? AC C++ Solution
LeetCode 102.Binary Tree Level Order Traversal (二叉樹的層次遍歷)
題目描述: 給定一個二叉樹,返回其按層次遍歷的節點值。 (即逐層地,從左到右訪問所有節點)。 例如: 給定二叉樹: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其層次遍歷結果: