[LeetCode] Flatten Binary Tree to Linked List, leetcodeflatten

Source: Internet
Author: User

[LeetCode] Flatten Binary Tree to Linked List, leetcodeflatten

Title: Flatten Binary Tree to Linked List

<Span style = "font-size: 18px;">/** LeetCode Flatten Binary Tree to Linked List *: Given a Binary Tree, transform it into a structure equivalent to a single-chain table. We can see that this structure is: The left son of each node is empty, and the right son points to the next node in the previous order * idea: when observed, we can get the correct sequence connected by * Definition for binary tree * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode (int x) {val = x;} */package javaTrain; public class Train18 {public void flatten (TreeNode root) {if (root = Null | (root. left = null & root. right = null) return; preOrder (root); return;} private TreeNode preOrder (TreeNode root) {if (root = null | (root. left = null & root. right = null) return root; TreeNode left = root. left; TreeNode right = root. right; TreeNode last; root. left = null; if (left! = Null) {root. right = left; last = preOrder (left); last. left = null; if (right! = Null) {last. right = right; return preOrder (right);} else return last;} else {root. right = right; return preOrder (right) ;}}</span>


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.