-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Leetcode 104 Maximum Depth of Binary Tree
- Loading branch information
1 parent
e050dd2
commit ec6b5fa
Showing
3 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
17 changes: 16 additions & 1 deletion
17
0104_Maximum Depth of Binary Tree/MaximumDepthofBinaryTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
0104_Maximum Depth of Binary Tree/MaximumDepthofBinaryTree2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
|
||
public class Solution { | ||
public int maxDepth(TreeNode root) { | ||
if(root == null) { | ||
return 0; | ||
} | ||
Queue<TreeNode> queue = new LinkedList<>(); | ||
queue.offer(root); | ||
int count = 0; | ||
while(!queue.isEmpty()) { | ||
int size = queue.size(); | ||
while(size-- > 0) { | ||
TreeNode node = queue.poll(); | ||
if(node.left != null) { | ||
queue.offer(node.left); | ||
} | ||
if(node.right != null) { | ||
queue.offer(node.right); | ||
} | ||
} | ||
count++; | ||
} | ||
return count; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
0104_Maximum Depth of Binary Tree/MaximumDepthofBinaryTree3.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
|
||
public class Solution { | ||
|
||
public int maxDepth(TreeNode root) { | ||
if(root == null) { | ||
return 0; | ||
} | ||
|
||
Stack<TreeNode> stack = new Stack<>(); | ||
Stack<Integer> value = new Stack<>(); | ||
stack.push(root); | ||
value.push(1); | ||
int max = 0; | ||
while(!stack.isEmpty()) { | ||
TreeNode node = stack.pop(); | ||
int temp = value.pop(); | ||
max = Math.max(temp, max); | ||
if(node.left != null) { | ||
stack.push(node.left); | ||
value.push(temp+1); | ||
} | ||
if(node.right != null) { | ||
stack.push(node.right); | ||
value.push(temp+1); | ||
} | ||
} | ||
return max; | ||
} | ||
} |