[LeetCode] questions conclustion_Path in Tree

Path in Tree:

 

[LeetCode] 112. Path Sum_Easy tag: DFS       input: root, target,   return True if exists sum(root-> leaf) == target else False       1

[LeetCode] 257. Binary Tree Paths_ Easy tag: DFS        input: root,   return all paths from root to leaves

[LeetCode] 113. Path Sum II      input: root, target, return all paths such that sum(root-> leaf) == target else []

 [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive  input: root, return max path sum such that any node -> any node.

[LeetCode] 437. Path Sum III_ Easy tag: DFS input: root, return all paths s.t a path sum == target from any node -> any child node .

[LeetCode] 687. Longest Univalue Path_Easy tag: DFS recursive  input:root, return max number of nodes s.t  a path any node -> any node and all nodes have same value, 

[LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive  input: root, return max number of nodes s.t a path any node -> child node and nodes are increasing consecutive order.      1

[LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive  input: root, return max number of nodes s.t a path any node -> any node and nodes are increasing or decreasing consecutive order. 

 

when it becomes path from any node -> any node

we define a helper function.

def helper(root):
    if not root: return 0
    l, r = helper(root.left), helper(root.right)
    l = l if l and condition else 0
                     # root.val == root.left.val    LC. 687
                     #  root.val + 1 == root.left.val    LC. 298
    r = r if r and condition else 0
    self.ans = max(self.ans, possible values)
    return max(possible values) # remember that path return should not be left and right both, only choose one.  

 

转载于:https://www.cnblogs.com/Johnsonxiong/p/9326898.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值