Binary Tree Tilt Solutions in C++
Number 563
Difficulty Easy
Acceptance 48.7%
Link LeetCode
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-tilt//// Author : liuyubobobo/// Time : 2019-02-23#include <iostream>using namespace std;/// Recursive Simulation/// Time Complexity: O(n^2)/// Space Complexity: O(h)/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:int findTilt(TreeNode* root) {if(!root) return 0;return abs(sum(root->left) - sum(root->right)) + findTilt(root->left) + findTilt(root->right);}private:int sum(TreeNode* root){if(!root) return 0;return root->val + sum(root->left) + sum(root->right);}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-tilt//// Author : liuyubobobo/// Time : 2019-02-23#include <iostream>using namespace std;/// Recursive and use class variable to record the result/// Time Complexity: O(n)/// Space Complexity: O(h)/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {private:int result;public:int findTilt(TreeNode* root) {result = 0;dfs(root);return result;}private:int dfs(TreeNode* root){if(!root) return 0;int left = dfs(root->left);int right = dfs(root->right);result += abs(left - right);return root->val + left + right;}};int main() {return 0;}