GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/Tree/687.Longest-Univalue-Path at master · wisdompeak/LeetCode

Expand file tree

/

687.Longest-Univalue-Path

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

687.Longest-Univalue-Path

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
687.Longest-Univalue-Path.cpp
687.Longest-Univalue-Path.cpp
 
 
Readme.md
Readme.md
 
 

Readme.md

687.Longest-Univalue-Path

此题和543. Diameter of Binary Tree很相似。设计DFS(node)函数,返回的是以node为起点,朝某个单向下行最长的Univalue path

每一次调用DFS(node),都要先调用L=DFS(node->left)和R=DFS(node->right),将左右节点都递归遍历完毕。

如果左节点、当前节点、右节点的数值相等,说明可以形成一个完整的uniValuePath,用L+R+1来刷新全局的longest变量。

那么考虑DFS(node)自己的返回值是什么呢?显然是1,或L+1(如果根与左节点相同),或R+1(如果根与右节点相同)

Leetcode Link

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.