Closed kymr closed 6 years ago
I have slightly modified the LeetCode example.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void printLeafValues(TreeNode root) {
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void printLeafValues(TreeNode root) {
if (root == null) {
return;
}
Stack<TreeNode> stack = new Stack<Treenode>();
stack.push(root);
while (stack.empty() == false) {
TreeNode current = stack.pop();
boolean isLeaf = current.left == null && current.right == null;
if (isLeaf) {
System.out.println(current.val);
}
if (current.left != null) {
stack.push(current.left)
}
if (current.right != null) {
stack.push(current.right);
}
}
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void printLeafValues(TreeNode root) {
if (root == null) {
return;
}
Stack<TreeNode> stack = new Stack<Treenode>();
stack.push(root);
while (stack.empty() == false) {
TreeNode current = stack.pop();
boolean isLeaf = current.left == null && current.right == null;
if (isLeaf) {
System.out.println(current.val);
}
if (current.right != null) {
stack.push(current.right);
}
if (current.left != null) {
stack.push(current.left)
}
}
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public void printLeafValues(TreeNode root) {
if (root == null) {
return;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
Set<TreeNode> addedSet = new HashSet<TreeNode>();
stack.push(root);
while (stack.empty() == false) {
TreeNode current = stack.peek();
boolean isLeftComplete = current.left == null || addedSet.contains(current.left);
if (isLeftComplete) {
stack.pop();
addedSet.add(current);
if (current.right != null) {
stack.push(current.right);
} else {
if (current.left == null) {
System.out.println(current.val);
}
{
} else {
stack.push(current.left);
}
}
}
}
There is very simple solution for this
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public class Solution {
public ArrayList<Integer> inorderTraversal(TreeNode root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ArrayList<Integer> lst = new ArrayList<Integer>();
if(root == null)
return lst;
Stack<TreeNode> stack = new Stack<TreeNode>();
//define a pointer to track nodes
TreeNode p = root;
while(!stack.empty() || p != null){
// if it is not null, push to stack
//and go down the tree to left
if(p != null){
stack.push(p);
p = p.left;
// if no left child
// pop stack, process the node
// then let p point to the right
}else{
TreeNode t = stack.pop();
lst.add(t.val);
p = t.right;
}
}
return lst;
}
}
Problem