binary-tree-preorder-traversal

/**
*
* @author gentleKay
* 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?
*
* 给定二叉树,返回其节点值的预排序遍历。
* 例如:
* 给定二叉树1,,2,3,
* 1

2
/
3
* 返回[1,2,3]。
* 注意:递归解决方案很简单,可以迭代吗?
*/

方法一: 递归

import java.util.ArrayList;

/**
 * 
 * @author gentleKay
 * 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?
 * 
 * 给定二叉树,返回其节点值的预排序遍历。
 * 例如:
 * 给定二叉树1,,2,3,
 * 1
    
     2
    /
   3
 * 返回[1,2,3]。
 * 注意:递归解决方案很简单,可以迭代吗?
 */

public class Main28 {
	public static void main(String[] args) {
		TreeNode root = new TreeNode(4);
		root.left = new TreeNode(2);
		root.left.left = new TreeNode(1);
		root.left.right  = new TreeNode(3);
		
		root.right = new TreeNode(6);
		root.right.left = new TreeNode(5);
		root.right.right = new TreeNode(7);
		root.right.right.right = new TreeNode(8);
		System.out.println(Main28.preorderTraversal(root));
	}
	
	public static class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;
		TreeNode(int x) { val = x; }
	}
	
	// 递归
	public static ArrayList<Integer> preorderTraversal(TreeNode root) { //[4, 2, 1, 3, 6, 5, 7, 8]
		if (root == null){
            return array;
        }
		ergodic(root);
		return array;
    }
	
	static ArrayList<Integer> array = new ArrayList<>();
	public static void ergodic(TreeNode root) {
		array.add(root.val);
        if (root.left != null) {
        	preorderTraversal(root.left);
        }
        if (root.right != null) {
        	preorderTraversal(root.right);
        }
	}
}

方法二: 非递归

import java.util.ArrayList;
import java.util.Stack;

/**
 * 
 * @author gentleKay
 * 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?
 * 
 * 给定二叉树,返回其节点值的预排序遍历。
 * 例如:
 * 给定二叉树1,,2,3,
 * 1
    
     2
    /
   3
 * 返回[1,2,3]。
 * 注意:递归解决方案很简单,可以迭代吗?
 */

public class Main28 {
	public static void main(String[] args) {
		TreeNode root = new TreeNode(4);
		root.left = new TreeNode(2);
		root.left.left = new TreeNode(1);
		root.left.right  = new TreeNode(3);
		
		root.right = new TreeNode(6);
		root.right.left = new TreeNode(5);
		root.right.right = new TreeNode(7);
		root.right.right.right = new TreeNode(8);
		System.out.println(Main28.preorderTraversal(root));
	}
	
	public static class TreeNode {
		int val;
		TreeNode left;
		TreeNode right;
		TreeNode(int x) { val = x; }
	}
	
	//非递归
	static ArrayList<Integer> array = new ArrayList<>(); //[4, 2, 1, 3, 6, 5, 7, 8]
	public static ArrayList<Integer> preorderTraversal(TreeNode root) {
		Stack<TreeNode> stack = new Stack<>();
		while (root != null || !stack.isEmpty()) {
			while (root != null) {
				array.add(root.val);
				stack.push(root);
				root = root.left;
			}
			if (!stack.isEmpty()) {
				root = stack.pop();
				root = root.right;
			}
		}
		
		return array;
    }

}

  

原文地址:https://www.cnblogs.com/strive-19970713/p/11283267.html