[Lintcode] Invert binary tree flips two forks

Source: Internet
Author: User
Tags lintcode

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.

Example

Given 4 points:,,, (1,2) (3,6) (0,0) (1,3) .

The maximum number is 3 .

Leecode on the original topic, see my previous blog Invert binary tree flipping two fork trees.

Solution One:

//recursionclassSolution { Public:    /** * @param root:a TreeNode, the root of the binary tree * @return: Nothing*/    voidInvertbinarytree (TreeNode *root) {        if(!root)return; TreeNode*tmp = root->Left ; Root->left = root->Right ; Root->right =tmp; Invertbinarytree (Root-Left ); Invertbinarytree (Root-Right ); }};

Solution Two:

//non-recursionclassSolution { Public:    /** * @param root:a TreeNode, the root of the binary tree * @return: Nothing*/    voidInvertbinarytree (TreeNode *root) {        if(!root)return; Queue<TreeNode*>Q;        Q.push (root);  while(!Q.empty ()) {TreeNode* node =Q.front (); Q.pop (); TreeNode*tmp = node->Left ; Node->left = node->Right ; Node->right =tmp; if(node->left) Q.push (node->Left ); if(node->right) Q.push (node->Right ); }    }};

[Lintcode] Invert binary tree flips two forks

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.