Skip to content

Latest commit

 

History

History
25 lines (22 loc) · 625 Bytes

binary_tree_tilt.md

File metadata and controls

25 lines (22 loc) · 625 Bytes

563. Binary Tree Tilt, kind of DFS

  • we have to do dfs on trees type of thing
  • we have been doing the same in calculating height of the tree and several more problems.
Code
class Solution {
  public:
  int ans = 0;
  int dfs(TreeNode* root) {
    int left = root -> left ? dfs(root -> left) : 0;
    int right = root -> right ? dfs(root -> right) : 0;
    ans += abs(left - right);
    return abs(left - right) + root -> val;
  }
  int findTilt(TreeNode* root) {
    int something = dfs(root);
    return ans;
  }
};