Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active September 28, 2019 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/15751618dd514385b2dd601155a3aca9 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/15751618dd514385b2dd601155a3aca9 to your computer and use it in GitHub Desktop.
maxDepth
/*
https://leetcode.com/problems/balanced-binary-tree/submissions/
Runtime: 16 ms, faster than 7.08% of Go online submissions for Balanced Binary Tree.
Memory Usage: 6.1 MB, less than 50.00% of Go online submissions for Balanced Binary Tree.
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
var ans bool
func abs(x int) int{
if x >= 0 {
return x;
}
return -x;
}
func max(x,y int) int{
if x > y {
return x
}
return y
}
func dfs(node * TreeNode, depth int)int{
if !ans{
return 0
}
if nil == node {
return depth
}
left := dfs(node.Left, depth+1)
right:= dfs(node.Right, depth+1)
if 1 < abs(left-right) {
ans = false
return 0
}
return max(left,right)
}
func isBalanced(root *TreeNode) bool {
ans = true
dfs(root,0)
return ans;
}
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# end
# @param {TreeNode} root
# @return {Integer}
# https://leetcode.com/problems/maximum-depth-of-binary-tree/submissions/
# Runtime: 32 ms, faster than 96.63% of Ruby online submissions for Maximum Depth of Binary Tree.
# Memory Usage: 13.9 MB, less than 33.33% of Ruby online submissions for Maximum Depth of Binary Tree.
def max_depth(root)
dfs = ->(x,dp){ x.nil? ? dp : [dfs[x.left,dp+1], dfs[x.right,dp+1]].max }
dfs[root, 0]
end
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
Runtime: 4 ms, faster than 95.10% of Go online submissions for Maximum Depth of Binary Tree.
Memory Usage: 4.4 MB, less than 83.33% of Go online submissions for Maximum Depth of Binary Tree.
https://leetcode.com/problems/maximum-depth-of-binary-tree/
*/
func dfs(node * TreeNode, depth int) int{
if nil == node {
return depth
}
left, right := dfs(node.Left, depth + 1), dfs(node.Right, depth + 1)
if left > right {
return left
}
return right
}
func maxDepth(root *TreeNode) int {
return dfs(root,0)
}
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
https://leetcode.com/problems/maximum-depth-of-binary-tree/submissions/
Runtime: 56 ms, faster than 91.27% of JavaScript online submissions for Maximum Depth of Binary Tree.
Memory Usage: 37.3 MB, less than 12.50% of JavaScript online submissions for Maximum Depth of Binary Tree.
*/
var maxDepth = function(root) {
let dfs = (node,dpt) => null == node ? dpt : Math.max(dfs(node.left,dpt+1),dfs(node.right,dpt+1));
return dfs(root,0)
};
<?php
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
Runtime: 4 ms, faster than 98.97% of PHP online submissions for Maximum Depth of Binary Tree.
Memory Usage: 16.8 MB, less than 33.33% of PHP online submissions for Maximum Depth of Binary Tree.
https://leetcode.com/problems/maximum-depth-of-binary-tree/submissions/
*/
class Solution {
function dfs($nd,$dt){
return null == $nd ? $dt : max($this->dfs($nd->left,$dt+1), $this->dfs($nd->right,$dt+1));
}
/**
* @param TreeNode $root
* @return Integer
*/
function maxDepth($root) {
return $this->dfs($root,0);
}
}
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
'''
https://leetcode.com/problems/maximum-depth-of-binary-tree/submissions/
Runtime: 52 ms, faster than 44.75% of Python3 online submissions for Maximum Depth of Binary Tree.
Memory Usage: 16.4 MB, less than 5.21% of Python3 online submissions for Maximum Depth of Binary Tree.
'''
class Solution:
def maxDepth(self, root: TreeNode) -> int:
dfs = lambda node,dpth: dpth if None is node else max(dfs(node.left,dpth+1),dfs(node.right,dpth+1))
return dfs(root,0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment