wupangyen / LeetCode

LeetCode
1 stars 0 forks source link

LeetCode 110. Balanced Binary Tree #30

Open wupangyen opened 3 years ago

wupangyen commented 3 years ago

We can use pre order traversal: Self Node, Left Child, Right Child

wupangyen commented 3 years ago

as we traverse left child we keep track of the num of left child, and afterwards as we are traverse right child we keep track of how many right child there is.

wupangyen commented 3 years ago

What is a Balanced Binary Tree: Summary:

at any node in the tree the height of left child and height of right child. the difference should not be more than one

wupangyen commented 3 years ago

Balanced Factor | height of left subtree - height of right subtree | <= 1

wupangyen commented 3 years ago

absolute of left height - right height

wupangyen commented 3 years ago

Summary: There are two approaches

  1. Top Down
  2. Bottom Up
wupangyen commented 3 years ago
Screen Shot 2021-09-01 at 4 16 40 PM
wupangyen commented 3 years ago

Bottom Up

wupangyen commented 3 years ago
Screen Shot 2021-09-01 at 4 29 53 PM