leetcode_144_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?

思路:

具体思路和中序遍历是一致的,只是访问结点的值的时机不同罢了,具体思路参见:http://blog.csdn.net/mnmlist/article/details/44312315

代码:

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
	     List<Integer>list=new ArrayList<Integer>();
	     if(root==null)
			 return list;
	     Stack<TreeNode>st=new Stack<TreeNode>();
	     st.push(root);
	     TreeNode top=null;
	     list.add(root.val);
	     while(!st.empty())
	     {
	    	 top=st.peek();
	    	 while(top.left!=null)
	    	 {
	    		 st.push(top.left);
	    		 list.add(top.left.val);
	    		 top=top.left;
	    	 }
	    	 while(top.right==null)
	    	 {

	    		 st.pop();
	    		 if(!st.empty())
	    			 top=st.peek();
	    		 else
	    			 break;
	    	 }
	    	 if(!st.empty())
	    	 {
		    	 st.pop();
		    	 st.push(top.right);
		    	 list.add(top.right.val);
	    	 }

	     }
	     return list;
	 }
}

结果:

时间: 2024-11-06 12:42:00

leetcode_144_Binary Tree Preorder Traversal的相关文章

[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: 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    /   3return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively?

40: Binary Tree Preorder Traversal

/************************************************************************/            /*       40:  Binary Tree Preorder Traversal                               */            /************************************************************************/ 

LeetCode:Binary Tree Preorder Traversal

题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode* left; 4 TreeNode* right; 5 TreeNode(int x): val(x), left(NULL),right(NULL) {} 6 }; 7 8 vector<int> preorderTraversal(TreeNode *root) //非递归的前序遍历(用栈实现)

[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 144. Binary Tree Preorder Traversal 解题报告

144. Binary Tree Preorder Traversal My Submissions Question Total Accepted: 108336 Total Submissions: 278322 Difficulty: Medium Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3

Binary Tree Preorder Traversal and 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]. c++版: /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode

[Leetcode][JAVA] 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? 不使用递归前序遍历,可以

Binary Tree Postorder Traversal &amp;&amp; Binary Tree Preorder Traversal

详见:剑指 Offer 题目汇总索引:第6题 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 y