563. Binary Tree Tilt

Source: Internet
Author: User

https://leetcode.com/problems/binary-tree-tilt/description/

Very good a topic, examining is not clear words are easy to do wrong. The main tilt of whole tree is defined as the sum of all node ' s tilt instead of the tilt of root taken for granted.

I thought it was simple. Tilt of root causes a complete error. The idea can actually be seen as a variant of the sum of children, but not only to track the sum of each subtree but also to accumulate their tilt.

/** Definition for a binary tree node. * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * TreeNode (int x): Val (x), left (null), right (NULL) {} *}; */classSolution { Public:    intTiltiter (treenode* root,int*t) {if(Root = =nullptr) {            return 0; }                intLeftsum =0; intRightsum =0; if(Root->left! =nullptr) {Leftsum= Tiltiter (root->Left , T); }        if(Root->right! =nullptr) {Rightsum= Tiltiter (root->Right , T); }        //Tilt of the current node;        intTilt = ABS (Leftsum-rightsum); *t + =tilt; returnRoot->val + leftsum +rightsum; }        intFindtilt (treenode*root) {        intTilt =0; Tiltiter (Root,&tilt); returntilt; }};

563. Binary Tree Tilt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.