Binary Tree Inorder Traversa
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Binary Tree Inorder Traversa相关的知识,希望对你有一定的参考价值。
?
package cn.edu.xidian.sselab.hashtable; import java.util.ArrayList; import java.util.List; import java.util.Stack; /** * * @author zhiyong wang * title: Binary Tree Inorder Traversal * content: * 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] * */ public class BinaryTreeInorderTraversal { //with stack to solve this question public List<Integer> inorderTraversal(TreeNode root){ TreeNode node = root; Stack<TreeNode> stack = new Stack<TreeNode>(); List<Integer> list = new ArrayList<Integer>(); while(node != null || !stack.isEmpty()){ while(node != null){ stack.push(node); node = node.left; } if(!stack.isEmpty()){ node = stack.pop(); list.add(node.val); node = node.right; } } return list; } //with recursive to solve this question //这个地方注意三个地方:(1)if(node == null)的时候,返回的是list,而不是null // (2) 题目中定义的是left与right,而不是leftNode,rightNode // (3)递归返回的结果要加到list中,所以用list.addAll(); public List<Integer> inorderTraversals(TreeNode root){ TreeNode node = root; List<Integer> list = new ArrayList<Integer>(); if(node == null) return list; list.addAll(inorderTraversals(node.left)); list.add(node.val); list.addAll(inorderTraversals(node.right)) ; return list; } } class TreeNode{ int val; TreeNode left; TreeNode right; TreeNode(int val){ this.val = val; } } |
以上是关于Binary Tree Inorder Traversa的主要内容,如果未能解决你的问题,请参考以下文章
94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal
(Tree)94.Binary Tree Inorder Traversal