Leetcode practice  0.0
Public Member Functions | List of all members
Binary_Tree Class Reference

#include <binary_tree.h>

Public Member Functions

TreeNodenewNode (int val)
 Allocate the dynamic memory to a new node and assign a value. More...
 
void printinOrder (TreeNode *root)
 Print tree nodes in In-order traversal. More...
 
TreeNodeinsertNode (int arr[], TreeNode *root, int idx, int size)
 Construct binary tree node from given array in level order. More...
 
bool findtwosumTarget (TreeNode *root, int target)
 Two Sum IV - Input is a BST - Easy. More...
 
void storinginOrder (TreeNode *root, vector< int > &num)
 In-order storing the binary tree value into vector. More...
 

Member Function Documentation

◆ findtwosumTarget()

bool Binary_Tree::findtwosumTarget ( TreeNode root,
int  target 
)

Two Sum IV - Input is a BST - Easy.

Parameters
Binarysearch tree
Targetnumber
Returns
True if there exists two elements such that their sum is equal to the given target

Ideal:

  1. Using recursive traversal to transfer tree node into vector array
  2. Determine whether there exists the sum of two value is same as target number
105 {
106  int i, j;
107  bool result = false;
108  vector<int> nums;
109 
110  // Transfer tree node into vector array
111  storinginOrder(root, nums);
112 
113  for (i = 0, j = (int) nums.size() - 1; i < j;) {
114  if (nums[i] + nums[j] == target)
115  result = true;
116 
117  nums[i] + nums[j] < target ? ++ i: -- j;
118  }
119  return result;
120 }
void storinginOrder(TreeNode *root, vector< int > &num)
In-order storing the binary tree value into vector.
Definition: binary_tree.cpp:45

Referenced by leetcode_practice().

◆ insertNode()

TreeNode * Binary_Tree::insertNode ( int  arr[],
TreeNode root,
int  idx,
int  size 
)

Construct binary tree node from given array in level order.

Parameters
Givenarray
Parentbinary tree node
Givenlevel
Totalsize of the given array
Returns
Constructed binary tree node

Ideal:

  1. Insert the left node firstly (0 -> 1 -> 3)
  2. Second, insert the right node firstly (4 -> 2)
  3. Insert the rest of node (5)

Input: { 5, 3, 6, 2, 4, 7} 5 / \ 3 6 / \ / 2 4 7

78 {
79  // protect to avoid over the range of array
80  if (idx < size) {
81 
82  // New node
83  TreeNode* root_tmp = newNode(arr[idx]);
84  root = root_tmp;
85 
86  // insert left side
87  root->left = insertNode(arr, root->left, 2*idx + 1, size);
88 
89  // insert right side
90  root->right = insertNode(arr, root->right, 2*idx + 2, size);
91  }
92  return root;
93 }
Definition: binary_tree.h:9
TreeNode_T * left
Definition: binary_tree.h:12
TreeNode * newNode(int val)
Allocate the dynamic memory to a new node and assign a value.
Definition: binary_tree.cpp:15
TreeNode * insertNode(int arr[], TreeNode *root, int idx, int size)
Construct binary tree node from given array in level order.
Definition: binary_tree.cpp:77
TreeNode_T * right
Definition: binary_tree.h:13

Referenced by leetcode_practice().

◆ newNode()

TreeNode * Binary_Tree::newNode ( int  val)

Allocate the dynamic memory to a new node and assign a value.

Parameters
Givenvalue
Returns
New node
16 {
17  TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
18  root->val = val;
19  root->left = root->right = NULL;
20 
21  return root;
22 }
Definition: binary_tree.h:9
TreeNode_T * left
Definition: binary_tree.h:12
TreeNode_T * right
Definition: binary_tree.h:13
int val
Definition: binary_tree.h:11

◆ printinOrder()

void Binary_Tree::printinOrder ( TreeNode root)

Print tree nodes in In-order traversal.

Parameters
Parentbinary tree node
Returns
None
30 {
31  if (root != NULL)
32  {
33  printinOrder(root->left);
34  cout << "" << root->val << endl;
35  printinOrder(root->right);
36  }
37 }
TreeNode_T * left
Definition: binary_tree.h:12
TreeNode_T * right
Definition: binary_tree.h:13
int val
Definition: binary_tree.h:11
void printinOrder(TreeNode *root)
Print tree nodes in In-order traversal.
Definition: binary_tree.cpp:29

◆ storinginOrder()

void Binary_Tree::storinginOrder ( TreeNode root,
vector< int > &  num 
)

In-order storing the binary tree value into vector.

Parameters
Parentbinary tree node
Vectordynamic array
Returns
None 0
46 {
47  // Protect infinite recursive loop
48  if (!root)
49  return;
50 
51  storinginOrder(root->left, num);
52  num.push_back(root->val);
53  storinginOrder(root->right, num);
54 }
void storinginOrder(TreeNode *root, vector< int > &num)
In-order storing the binary tree value into vector.
Definition: binary_tree.cpp:45
TreeNode_T * left
Definition: binary_tree.h:12
TreeNode_T * right
Definition: binary_tree.h:13
int val
Definition: binary_tree.h:11

The documentation for this class was generated from the following files: