leetcode之Binary Tree Preorder Traversal (前序),中序,后续。非递归,递归

1:前序遍历(根,左,右)

递归的方法很简单:

public static void pr(TreeNode root){
  if(root!=null){
	System.out.println(root.val);
	pr(root.left);
	pr(root.right);
  }
}

  非递归的方法:可以借助栈,入栈的顺序分别为右,左,根。

代码如下:

public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ls = new ArrayList<Integer>();
		if (root == null) {
			return ls;
		}
		Stack<TreeNode> st = new Stack<TreeNode>();
		st.push(root);
		while (!st.isEmpty()) {
			TreeNode temp = st.pop();
			ls.add(temp.val);
			if (temp.right != null) {
				st.push(temp.right);
			}
			if (temp.left != null) {
				st.push(temp.left);

			}
		}
		return ls;
    }

  2:中序遍历(左,根,右)

递归的方法:

public static void m(TreeNode root){
		if(root!=null){

			m(root.left);
			System.out.println(root.val);
			m(root.right);
		}
	}

  非递归的方法:

首先要非常明白中序遍历一定是最先访问最左的结点,用栈来帮助实现。要访问某个结点,那在这个结点入栈之前一定将它所有的左子树结点全部入栈,然后每出栈一个节点将这个结点的有节点进栈,因为这个右子树也有可能有左子树,下面附上代码:

public List<Integer> inorderTraversal(TreeNode root) {
        Stack<TreeNode> st = new Stack<TreeNode>();
        List<Integer> result = new ArrayList<Integer>();
        TreeNode cur = root;
        while(cur!=null||!st.empty()){
            while(cur!=null){
                st.push(cur);
                cur = cur.left;
            }
            cur = st.pop();
            result.add(cur.val);
            cur = cur.right;
        }
        return result;
    }

  3:后序遍历(左右根)

递归的方法,代码如下:

public ArrayList<Integer> postorderTraversal(TreeNode root) {
    ArrayList<Integer> result = new ArrayList<Integer>();

    if (root == null) {
        return result;
    }

    result.addAll(postorderTraversal(root.left));
    result.addAll(postorderTraversal(root.right));
    result.add(root.val);

    return result;
}

  非递归的方法,也是借助栈来完成,此时入栈的顺序为根,右,左。代码如下:

public List<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<Integer>();
		Stack<TreeNode> stack = new Stack<TreeNode>();
		TreeNode prev = null; // previously traversed node
		TreeNode curr = root;

		if (root == null) {
			return result;
		}

		stack.push(root);
		while (!stack.empty()) {
			curr = stack.peek();
			if (prev == null || prev.left == curr || prev.right == curr) {
				if (curr.left != null) {
					stack.push(curr.left);
				} else if (curr.right != null) {
					stack.push(curr.right);
				}
			} else if (curr.left == prev) { // traverse up the tree from the
											// left
				if (curr.right != null) {
					stack.push(curr.right);
				}
			} else { // traverse up the tree from the right
				result.add(curr.val);
				stack.pop();
			}
			prev = curr;
		}

		return result;
    }

  可参考网址:http://www.tuicool.com/articles/Yz2QJn

http://blog.csdn.net/kerryfish/article/details/24309617

时间: 2024-10-14 05:07:47

leetcode之Binary Tree Preorder Traversal (前序),中序,后续。非递归,递归的相关文章

[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 树 Binary Tree Preorder Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Preorder Traversal Total Accepted: 17948 Total Submissions: 51578 Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 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: Recur

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

[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 二叉树的前序遍历,同样使用栈来解,代码如下: 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 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

【LeetCode】Binary Tree Preorder Traversal (2 solutions)

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? 解法一:递归 /** * De

LeetCode OJ: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 /**