赞
踩
返回列表版本:辅助函数携带结果列表
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
inorderTraversalHelper(root,result);
return result;
}
void preorderTraversalHelper(TreeNode root,List<Integer> result){
if(root == null) return;
result.add(root.val);
inorderTraversalHelper(root.left,result);
inorderTraversalHelper(root.right,result);
return;
}
返回列表版本:设置全局变量
List<Integer> result = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
inorderTraversalHelper(root,result);
return result;
}
void preorderTraversalHelper(TreeNode root){
if(root == null) return;
result.add(root.val);
inorderTraversalHelper(root.left,result);
inorderTraversalHelper(root.right,result);
return;
}
public List<Integer> preorderTraversal(TreeNode root) {
inorderTraversalHelper(root,result);
return result;
}
void preorderTraversal(TreeNode root){
if(root == null) return;
System.out.print(root.val);
inorderTraversalHelper(root.left,result);
inorderTraversalHelper(root.right,result);
return;
}
class Solution { class TraverBox{ List<Integer> list; TraverBox(){ list = new ArrayList<>(); } void preorderTraversalHelper(TreeNode root) { list.add(root.val); } void preTraverHelper(TreeNode root) { if(root == null) return; list.add(root.val); preTraverHelper(root.left); preTraverHelper(root.right); } List<Integer> preTraver(TreeNode root) { preTraverHelper(root); return list; } } public List<Integer> preorderTraversal(TreeNode root) { TraverBox tox = new TraverBox(); tox.preTraver(root); return tox.list; } }
public List<Integer> preorderTraversal(TreeNode root) { List<Integer> result = new LinkedList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode subRoot = new TreeNode(); if (root != null) stack.push(root); //将根结点入栈 while(!stack.isEmpty()){ subRoot = stack.pop(); //弹出获取栈顶结点 if(subRoot != null){ //===右=== if(subRoot.right != null){ // 添加右结点(空结点不入栈) stack.push(subRoot.right); } //===左=== if(subRoot.left != null){ // 添加左节点(空结点不入栈) stack.push(subRoot.left); } //===中=== stack.push(subRoot); // 添加中结点 stack.push(null); // 中结点访问过,但是还没有处理,加入空结点做为标记。 }else{ // 只有遇到空结点的时候,才将下一个结点放进结果集 result.add(stack.pop().val); //重新取出栈中元素,加入到结果集 } } return result; }
返回列表版本:辅助函数携带结果列表
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
inorderTraversalHelper(root,result);
return result;
}
void preorderTraversalHelper(TreeNode root,List<Integer> result){
if(root == null) return;
inorderTraversalHelper(root.left,result);
result.add(root.val);
inorderTraversalHelper(root.right,result);
return;
}
返回列表版本:设置全局变量
List<Integer> result = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
inorderTraversalHelper(root,result);
return result;
}
void preorderTraversalHelper(TreeNode root){
if(root == null) return;
inorderTraversalHelper(root.left,result);
result.add(root.val);
inorderTraversalHelper(root.right,result);
return;
}
void preorderTraversal(TreeNode root){
if(root == null) return;
inorderTraversalHelper(root.left,result);
System.out.print(root.val);
inorderTraversalHelper(root.right,result);
return;
}
void inOrderNonRecur(){ Stack<TreeNode> stack = new Stack<>(); TreeNode subRoot = root; if (subRoot != null) { while (!stack.isEmpty() || subRoot != null) { if (subRoot != null) { stack.push(subRoot); subRoot = subRoot.left; } else { subRoot = stack.pop(); System.out.print("【"+subRoot.val+"】"); subRoot = subRoot.right; } } } }
带注释版本
public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new LinkedList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode subRoot = new TreeNode(); if (root != null) stack.push(root); while (!stack.isEmpty()) { subRoot = stack.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中 if (subRoot != null) { //===右=== if(subRoot.right != null){ // 添加右节点(空节点不入栈) stack.push(subRoot.right); } //===中=== stack.push(subRoot); // 添加中节点 stack.push(null); // 中节点访问过,但是还没有处理,加入空节点做为标记。 //===左=== if(subRoot.left != null){ // 添加左节点(空节点不入栈) stack.push(subRoot.left); } } else { // 只有遇到空节点的时候,才将下一个节点放进结果集 result.add(stack.pop().val); // 加入到结果集 } } return result; }
无注释版本
public List<Integer> inorderTraversal(TreeNode root) { List<Integer> result = new LinkedList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode subRoot = new TreeNode(); if (root != null) stack.push(root); while (!stack.isEmpty()) { subRoot = stack.pop(); if (subRoot != null) { if(subRoot.right != null) stack.push(subRoot.right); stack.push(subRoot); stack.push(null); if(subRoot.left != null) stack.push(subRoot.left); } else { result.add(stack.pop().val); } } return result; }
返回列表版本:辅助函数携带结果列表
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
inorderTraversalHelper(root,result);
return result;
}
void preorderTraversalHelper(TreeNode root,List<Integer> result){
if(root == null) return;
inorderTraversalHelper(root.left,result);
inorderTraversalHelper(root.right,result);
result.add(root.val);
return;
}
返回列表版本:设置全局变量
List<Integer> result = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
inorderTraversalHelper(root,result);
return result;
}
void preorderTraversalHelper(TreeNode root){
if(root == null) return;
inorderTraversalHelper(root.left,result);
inorderTraversalHelper(root.right,result);
result.add(root.val);
return;
}
void preorderTraversal(TreeNode root){
if(root == null) return;
inorderTraversalHelper(root.left,result);
inorderTraversalHelper(root.right,result);
System.out.print(root.val);
return;
}
public List<Integer> postorderTraversal(TreeNode root) { List<Integer> result = new ArrayList<>(); if (root != null) { Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); TreeNode subRoot = null; while (!stack.isEmpty()) { subRoot = stack.peek(); if (subRoot.left != null && root != subRoot.left && root != subRoot.right) { stack.push(subRoot.left); } else if (subRoot.right != null && root != subRoot.right) { stack.push(subRoot.right); } else { result.add(stack.pop().val); root = subRoot; } } } return result; }
public List<Integer> postOrderNonRecurByTwoStack(){ List<Integer> result = new ArrayList<>(); TreeNode subRoot = root; if (subRoot != null) { Stack<TreeNode> s1 = new Stack<TreeNode>(); Stack<TreeNode> s2 = new Stack<TreeNode>(); s1.push(subRoot); while (!s1.isEmpty()) { subRoot = s1.pop(); s2.push(subRoot); if (subRoot.left != null) { s1.push(subRoot.left); } if (subRoot.right != null) { s1.push(subRoot.right); } } while (!s2.isEmpty()) { result.add(s2.pop().val); } } }
public List<Integer> preorderTraversal(TreeNode root) { List<Integer> result = new LinkedList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode subRoot = new TreeNode(); if (root != null) stack.push(root); //将根结点入栈 while(!stack.isEmpty()){ subRoot = stack.pop(); //弹出获取栈顶结点 if(subRoot != null){ //===中=== stack.push(subRoot); // 添加中结点 stack.push(null); // 中结点访问过,但是还没有处理,加入空结点做为标记。 //===右=== if(subRoot.right != null){ // 添加右结点(空结点不入栈) stack.push(subRoot.right); } //===左=== if(subRoot.left != null){ // 添加左节点(空结点不入栈) stack.push(subRoot.left); } }else{ // 只有遇到空结点的时候,才将下一个结点放进结果集 result.add(stack.pop().val); //重新取出栈中元素,加入到结果集 } } return result; }
class Solution { public int[] levelOrder(TreeNode root) { //if(root == null) return new int[]{}; ArrayList<Integer> result = new ArrayList<Integer>(); Queue<TreeNode> queue = new LinkedList<TreeNode>(); TreeNode subRoot = new TreeNode(); if(root != null) queue.offer(root); while(!queue.isEmpty()){ subRoot = queue.poll(); result.add(subRoot.val); if(subRoot.left != null) queue.add(subRoot.left); if(subRoot.right != null) queue.add(subRoot.right); } int[] dest = new int[result.size()]; for(int i = 0 ; i < result.size() ; i++){ dest[i] = result.get(i); } return dest; } }
public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> ret = new ArrayList<List<Integer>>(); if (root == null) { return ret; } Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.offer(root); while (!queue.isEmpty()) { List<Integer> level = new ArrayList<Integer>(); int currentLevelSize = queue.size(); for (int i = 1; i <= currentLevelSize; ++i) { TreeNode node = queue.poll(); level.add(node.val); if (node.left != null) { queue.offer(node.left); } if (node.right != null) { queue.offer(node.right); } } ret.add(level); } return ret; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。