给定二叉查找树的根结点指针root,现编写一个方法,将二叉查找树转换为链表,其中二叉查找树的数据结构用TreeNode实现(包含了val属性和指向其它结点的指针),eft指针表示左儿子,right指针表示右儿子。请返回生成的链表的头指针。
加载中...
import java.util.*; /* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ /* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } }*/ public class Converter { public ListNode treeToList(TreeNode root) { // write code here } }
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ /* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {} };*/ class Converter { public: ListNode* treeToList(TreeNode* root) { // write code here } };
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x # self.next = None # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Converter: def treeToList(self, root): # write code here
/* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } }*/ /* public class ListNode { public int val; public ListNode next; public ListNode (int x) { val = x; } }*/ class Converter { public ListNode treeToList(TreeNode root) { // write code here } }