Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Tree_inorder_recursive
//Title of this code //'main' method must be in a class 'Rextester'. //Compiler version 1.7.0_51 import java.util.*; import java.lang.*; class Rextester { public static void main(String args[]) { TreeNode root=new TreeNode(10); TreeNode n2=new TreeNode(1); TreeNode n3=new TreeNode(7); TreeNode n4=new TreeNode(4); TreeNode n5=new TreeNode(9); TreeNode n0=new TreeNode(12); TreeNode n6=new TreeNode(15); TreeNode n7=new TreeNode(20); root.left=n4; root.right=n6; n4.left=n2; n4.right=n3; n3.right=n5; n6.left=n0; n6.right=n7; ArrayList<Integer> res=new ArrayList<Integer>(); if (root==null) return; inorderIter(root, res); //inorderTrav(root, res); //System.out.println("Hello, World!"); } static class TreeNode { TreeNode left; TreeNode right; int val; TreeNode(int v){val=v;} } private static void inorderTrav(TreeNode node, ArrayList<Integer> res) { if (node.left!=null) { inorderTrav(node.left,res); } if (node.right!=null) { inorderTrav(node.right,res); } res.add(node.val); System.out.print(node.val); System.out.print(','); return; } private static void inorderIter(TreeNode root, ArrayList<Integer> res) { Stack<TreeNode> st=new Stack<TreeNode>(); TreeNode tmp=root; while (tmp!=null||!st.isEmpty()) { if (tmp!=null) { st.push(tmp); tmp=tmp.left; } else { tmp=st.pop(); res.add(tmp.val); System.out.print(tmp.val); System.out.print(','); tmp=tmp.right; } } } }
run
|
edit
|
history
|
help
0
classwork
jb11.0 threads tick tock
Test processing time (loop vs. contains)
MAP List Iterator
JAVA # Dizi ile Fibonaççi
Iterative Merge sort
練習3-6
Coding For Fandroid
4c
Java - Swap two numbers without using temp var