二叉树的创建及非递归遍历

二叉树创建

/**
	 * 递归构建二叉树
	 * 
	 * @param arr
	 * @param index
	 * @return
	 */
	private static TreeNode createTree(String[] arr, int index) {
		TreeNode tn = null;
		if (index < arr.length) {
			if (arr[index].equals("null")) {
				return null;
			}
			Integer val = Integer.valueOf(arr[index]);
			tn = new TreeNode(val);
			// 构建二叉树左子树
			tn.left = createTree(arr, 2 * index + 1);
			// 构建二叉树右子树
			tn.right = createTree(arr, 2 * index + 2);
		}
		return tn;
	}

二叉树先序遍历

/**
	 * 先序遍历非递归
	 * @param root
	 */
	public void preOrderNone(TreeNode root) {
		Stack<TreeNode> stack = new Stack<>();
		stack.push(root);
		while(!stack.isEmpty()) {
			root = stack.pop();
			System.out.println(root.val);
			//先压入右子节点,再压入左节点
			if(root.right!=null) {
				stack.push(root.right);
			}
			if(root.left!=null) {
				stack.push(root.left);
			}
		}
	}

二叉树中序遍历

/**
	 * 
	 * 中序遍历
	 * @param root
	 */
	public void inOrder(TreeNode node) {
		Stack<TreeNode> stack = new Stack<>();
		while(node!=null || !stack.isEmpty()) {
			while(node!=null) {
				stack.push(node);
				node = node.left;
			}
			if(!stack.isEmpty()) {
				node = stack.pop();
				System.out.println(node.val);
				node = node.right;
			}
		}
	}

后序遍历

/**
	 * 后序遍历
	 * @param root
	 */
	private void postOrder(TreeNode root) {
		Stack<TreeNode> stack = new Stack<>();
		Stack<Integer> numStack = new Stack<>();   //辅助栈
		int left = 1;   //左节点
		int right = 2;  //右节点
		while(root!=null || !stack.isEmpty()) {
			while(root!=null) {
				//将节点入栈,并在辅助栈将节点标记为左节点
				stack.push(root);
				numStack.push(left);
				root = root.left;
			}
			
			if(!stack.isEmpty() && numStack.peek() == right) {
				//如果是从右子节点返回父节点,则遍历完成,弹出栈顶节点
				numStack.pop();
				System.out.println(stack.pop().val);
			}
			
			if(!stack.isEmpty() && numStack.peek() == left) {
				//如果是从左子节点返回父节点,则将标记改为右节点
				numStack.pop();
				numStack.push(right);
				root = stack.peek().right;
			}
			
		}
	}

层序遍历

//层序遍历二叉树
	public void levelOrder(TreeNode root) {
	//队列辅助
		Queue<TreeNode> queue = new LinkedList<>();
		queue.add(root);
		System.out.println(root.val);
		while(!queue.isEmpty()) {
			TreeNode node = queue.poll();
			if(null!=node.left) {
				queue.add(node.left);
			}
			if(null!=node.right) {
				queue.add(node.right);
			}
		}
	}
	
	
	public void levelOrder1(TreeNode root) {
		LinkedList<TreeNode> list = new LinkedList<>();
		list.add(root);
		while (!list.isEmpty()) {
			root = list.poll();
			System.out.println(root.val);
			if (root.left != null)
				list.offer(root.left);
			if (root.right != null)
				list.offer(root.right);
		}
	}

辅助方法

 private static String[] stringToArray(String str) {
		if (null != str && str.equals("")) {
			return null;
		}
		String[] fields = str.split(",");
		fields[0] = fields[0].substring(1);
		int lastIndex = fields.length - 1;
		fields[lastIndex] = fields[lastIndex].substring(0, 1);
		return fields;
	}

main方法

public static void main(String[] args) {
		TreeNode root = stringToTree("[3,9,20,2,null,15,7]");
		//new StringToBinaryTree().preOrder(root);
		//new StringToBinaryTree().levelOrder1(root);
		//new StringToBinaryTree().inOrder(root);
		new StringToBinaryTree().postOrder(root);
	}
public static TreeNode stringToTree(String str) {
		String[] elems = stringToArray(str);
		if (null == elems) {
			return null;
		}

		TreeNode root = createTree(elems, 0);
		return root;
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值